<?php
use app\tools\Re;
use yii\helpers\ArrayHelper;

$params = array_merge(
    require(__DIR__ . '/params.php')
);

$db = require __DIR__ . '/db.php';

$config = [
    'id' => 'basic',
    'basePath' => dirname(__DIR__),
    'bootstrap' => ['log'],
    'language' => 'zh-CN',
    'defaultRoute' => 'index',
    'aliases' => [
        '@bower' => '@vendor/bower-asset',
        '@npm'   => '@vendor/npm-asset',
    ],
    'components' => [
        'wechat' => [
            'class' => 'jianyan\easywechat\Wechat',
            'userOptions' => [],  // 用户身份类参数
            'sessionParam' => 'wechatUser', // 微信用户信息将存储在会话在这个密钥
            'returnUrlParam' => '_wechatReturnUrl', // returnUrl 存储在会话中
            'rebinds' => [ // 自定义服务模块
                // 'cache' => 'common\components\Cache',
            ]
        ],
        'request' => [
            // !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
            'cookieValidationKey' => 'VSEk4R4XxtLvpkp634mlK1JH0DBaiEy7',
        ],
        'response' => [
            'class' => 'yii\web\Response',
            'on beforeSend' => function ($event) {
                /** @var \yii\web\Response $response */
                $response = $event->sender;
                if($response->format == $response::FORMAT_HTML){
                    return;
                }
                if ($response->data !== null) {
                    if(!$response->isSuccessful){
                        if(isset($response->data['status']) && $response->data['status'] == 401){
                            $code = 401;
                        }else{
                            $code = Re::ERROR;
                        }
                        $response->data = [
                            'code' => $code,
                            'text' => isset($response->data['message'])?$response->data['message'].' '.$response->statusCode:$response->statusCode,
                        ];
                    }else{
                        if(ArrayHelper::isIndexed($response->data)){
                            $response->data = Re::data(['data'=>$response->data]);
                        }else{
                            $response->data = Re::data($response->data);
                        }
                    }
                    $response->statusCode = 200;
                }
            },
        ],
        'cache' => [
            'class' => 'yii\caching\FileCache',
        ],
        'user' => [
            'identityClass' => 'app\models\Identity',
            'enableAutoLogin' => true,
        ],
        'errorHandler' => [
            'errorAction' => 'site/error',
        ],
        'mailer' => [
            'class' => 'yii\swiftmailer\Mailer',
            // send all mails to a file by default. You have to set
            // 'useFileTransport' to false and configure a transport
            // for the mailer to send real emails.
            'useFileTransport' => false,
            'transport' => [
                'class' => 'Swift_SmtpTransport',
                'host' => 'smtp.qq.com',  //每种邮箱的host配置不一样
                'username' => '995284084@qq.com',
                'password' => 'msbljqvwvenjbcee',
                'port' => '465',
                'encryption' => 'ssl',
            ],

        ],
        'log' => [
            'traceLevel' => YII_DEBUG ? 3 : 0,
            'targets' => [
                [
                    'class' => 'yii\log\FileTarget',
                    'levels' => ['error', 'warning'],
                ],
//                [
//                    'class' => 'yii\log\FileTarget',
//                    'levels' => ['error', 'warning', 'info', 'trace'],
//                    'categories' => ['yii\db\*'],
//                    'logFile' => '@runtime/logs/db.log'
//                ],
            ],
        ],
        'redis' => [
            'class' => 'yii\redis\Connection',
            'hostname' => '127.0.0.1',
            'port' => 6379,
            'database' => 0,
        ],
        'db' => $db,
        'urlManager' => [
            'enablePrettyUrl' => true,
            'showScriptName' => true,
            'rules'=>array(
                'test/oauth/<appid:[a-zA-Z0-9]+>/<template_id:[\w]+>/<redirect_url:\S+>' => 'test/oauth',
                'test/oauths/<redirect_url:\S+>' => 'test/oauths'
            ),
        ],
        'authManager' => [
            'class' => 'yii\rbac\DbManager',
            // uncomment if you want to cache RBAC items hierarchy
            // 'cache' => 'cache',
        ],
    ],
    'modules' => [
        'admin' => [
            'class' => 'app\modules\admin\Module',
        ],
        'backend' => [
          'class' => 'app\modules\backend\Module',
        ],
        'rbac' => [
            'class' => 'mdm\admin\Module',
            'layout' => 'left-menu'
        ]
    ],
    'params' => $params,
];
if (!YII_ENV_TEST) {
    // configuration adjustments for 'dev' environment
    $config['bootstrap'][] = 'debug';
    $config['modules']['debug'] = [
        'class' => 'yii\debug\Module',
        'allowedIPs' => ['*']
    ];

    $config['bootstrap'][] = 'gii';
    $config['modules']['gii'] = [
        'class' => 'yii\gii\Module',
        'allowedIPs' => ['*']
//远程时需要添加ip段

    ];

}
if (YII_ENV_DEV) {
    // configuration adjustments for 'dev' environment
//    $config['bootstrap'][] = 'debug';
//    $config['modules']['debug'] = [
//        'class' => 'yii\debug\Module',
        // uncomment the following to add your IP if you are not connecting from localhost.
//        'allowedIPs' => ['127.0.0.1', '::1'],
//    ];

    $config['bootstrap'][] = 'gii';
    $config['modules']['gii'] = [
        'class' => 'yii\gii\Module',
        // uncomment the following to add your IP if you are not connecting from localhost.
        'allowedIPs' => ['*', '127.0.0.1', '::1'],
//        'generators' => [
//            'crud' => [ //生成器名称
//                'class' => 'yii\gii\generators\crud\Generator',
//                'templates' => [ //设置我们自己的模板
//                    //模板名 => 模板路径
//                    'myCrud' => '@app/giitemplate/crud/default',
//                ]
//            ]
//        ],
    ];
}

return $config;
